<template>
	<view class="container">
		<tui-tabs :tabs="tabs" :isFixed="scrollTop>=0" :currentTab="currentTab" selectedColor="#E41F19"
			sliderBgColor="#E41F19" @change="change" itemWidth="20%"></tui-tabs>
		<!--选项卡逻辑自己实现即可，此处未做处理-->
		<view :class="{'tui-order-list':scrollTop>=0}" v-if="list && list.length>0">  
			<view class="tui-order-item" v-for="(model,orderIndex) in list" :key="orderIndex">
				<tui-list-cell :hover="false" :lineLeft="false">
					<view class="tui-goods-title">
						<view>订单号：{{ model.order_sn }}</view>
						<view class="tui-order-status">{{ model.status_text }}</view>
					</view>
				</tui-list-cell>
				<block v-for="(itemSub,index) in model.items" :key="index">
					<tui-list-cell padding="0">
						<view class="tui-goods-item" @click.stop="detail(model)">
							<image :src="info.app.cdnurl+itemSub.goods_image" class="tui-goods-img"></image>
							<view class="" style="display: flex;flex-direction: column;flex: 1;">
								<view class="" style="flex: 1;display: flex;justify-content: space-between;">
									<view class="tui-goods-center">
										<view class="tui-goods-name">{{itemSub.goods_title}}</view>
										<view class="tui-goods-attr">{{itemSub.goods_sku_text}}</view>
									</view>
									<view class="tui-price-right">
										<view>￥{{itemSub.goods_price}}</view>
										<view>x{{itemSub.goods_num}}</view>


									</view>

								</view>
								<view class="tui-order-btn" style="margin-top: 20rpx;padding: 20rpx 0;" v-if="itemSub.btns.length>=1">
									<!-- <view class="tui-btn-ml" style="margin-left: 10rpx;" v-for="(btnItem,k) in itemSub.btns" :key="k"></view> -->
										<view class="acBtn" v-if="itemSub.btns.includes('re_aftersale')" @click.stop="refund(itemSub)">重新售后</view>
										<view class="acBtn" v-if="itemSub.btns.includes('aftersale')" @click.stop="refund(itemSub)">申请售后</view>
										<view class="acBtn" v-if="itemSub.btns.includes('aftersale_info')" @click.stop="refundDetail(itemSub)">售后详情</view>
								</view>
							</view>
						</view>
					</tui-list-cell>
				</block>
				<tui-list-cell :hover="false" unlined>
					<view class="tui-goods-price">
						<!-- <view>共4件商品 合计：</view> -->
						<view class="tui-size-24 marginright" style="color: #999999;">优惠：{{ model.total_discount_fee }}
						</view>
						<view class="tui-size-24 marginright" style="color: #999999;">运费：{{ model.dispatch_amount }}
						</view>
						<view class="tui-size-24">￥</view>
						<view class="tui-price-large">总金额：{{model.pay_fee}}</view>
					</view>
				</tui-list-cell>
				<view class="tui-order-btn" style="background: #fff;">

					<view
						style="border: 1px solid rgba(0, 0, 0, 0.2);padding: 10rpx 25rpx;color: #000;font-size: 22rpx;border-radius:30rpx"
						v-if="model.btns.length === 0" @click.stop="detail(model)">查看详情</view>

					<view class="tui-btn-ml" v-for="(item,index) in model.btns" :key="index" v-else>
						<tui-button type="danger" plain width="152rpx" height="56rpx" :size="26" shape="circle"
							v-if="item=='cancel'" @click="cancel(model)">取消订单</tui-button>

						<tui-button type="danger" plain width="152rpx" height="56rpx" :size="26" shape="circle"
							v-if="item=='pay'" @click="payOrderDetail(model)">继续支付</tui-button>
						<tui-button type="danger" plain width="152rpx" height="56rpx" :size="26" shape="circle"
							style="background: #f6f6f6;border: none;" v-if="item=='express'"
							@click="expressLog(model.items[0])">查看物流</tui-button>

						<tui-button type="danger" plain width="152rpx" height="56rpx" :size="26" shape="circle"
							v-if="item=='comment'" @click="addEvaluate(model)">评价晒单</tui-button>

						<tui-button type="danger" plain width="152rpx" height="56rpx" :size="26" shape="circle"
							v-if="item=='confirm'" @click="confim(model)">确认收货</tui-button>

						<tui-button type="danger" plain width="152rpx" height="56rpx" :size="26" shape="circle"
							v-if="item=='apply_refund'" @click="delMoney(model)">申请退款</tui-button>

						<tui-button type="danger" plain width="152rpx" height="56rpx" :size="26" shape="circle"
							v-if="item=='delete'" @click="deletes(model)">删除订单</tui-button>

					</view>
				</view>
			</view>
		

		</view>
		<view v-else style="position: absolute;top: 50%;left: 50%;transform: translate(-50%,-50%);">
			 <tn-empty mode="order"></tn-empty>
		</view>
		<!--加载loadding-->
		<tui-loadmore v-if="loadding" :index="3" type="red"></tui-loadmore>
		<tui-nomore v-if="!pullUpOn" backgroundColor="#fafafa"></tui-nomore>
		<!--加载loadding-->
		<uni-popup ref="alertDialog" type="dialog">
			<uni-popup-dialog cancelText="关闭" confirmText="确定" title="提示" content="确定要删除订单吗?" @confirm="dialogConfirm"
				@close="dialogClose"></uni-popup-dialog>
		</uni-popup>

		<uni-popup ref="cancelDialog" type="dialog">
			<uni-popup-dialog cancelText="关闭" confirmText="确定" title="提示" content="确定要取消订单吗?" @confirm="dialogCancel"
				@close="dialogClose"></uni-popup-dialog>
		</uni-popup>



	</view>
</template>

<script>
	import {
		getOrder,
		getOrderConfirm,
		getOrderDelete,
		getOrderCancel,
		getOrderApplyRefundMoney
	} from "@/apis/index"
	export default {
		data() {
			return {
				tabs: [{
					name: "全部",
					type: 'all'
				}, {
					name: "待付款",
					type: 'unpaid'
				}, {
					name: "待发货",
					type: 'nosend'
				}, {
					name: "待收货",
					type: 'noget'
				}, {
					name: "待评价",
					type: 'nocomment'
				}, ],
				currentTab: 0,
				pageIndex: 1,
				loadding: false,
				pullUpOn: true,
				scrollTop: 0,
				type: null,
				num: 5,
				page: 1,
				list: [],
				lastPage: null,
				currentItem: {}
			}
		},
		async onLoad(option) {
			if (option.index) {
				this.type = this.tabs[option.index].type
				this.currentTab = Number(option.index)
				let res = await this.getList()
				this.list = res.data.data
				this.lastPage = res.data.last_page
			}

		},
		async onShow() {
			// this.page = 1
			// this.type = this.tabs[this.currentTab].type
			// let res = await this.getList()
			// this.list = res.data.data
			// this.lastPage = res.data.last_page
		},
		methods: {
			expressLog(item) {
				uni.navigateTo({
					url: `/templatePage/logistics/logistics?id=${item.id}&orderId=${item.order_id}`
				})
			},
			payOrderDetail(item) {
				uni.navigateTo({
					url: `/templatePage/orderDetail/orderDetail?order_sn=${item.order_sn}`
				})
			},
			//售后
			refund(item) {
				uni.navigateTo({
					url: `/templatePage/refund/refund?detail=${JSON.stringify(item)}`
				})
			},
			//售后详情
			refundDetail(item) {

				uni.navigateTo({
					url: `/templatePage/refundDetail/refundDetail?id=${item.ext.aftersale_id}`
				})
			},

			//退款
			async delMoney(item) {

				uni.showModal({
						title: '提示',
						content: '确定要退款吗?',
						success: async (res) => {
							if (res.confirm) {
								let res = await getOrderApplyRefundMoney(item.id)
								if (res.code == 1) {
									this.page = 1
									this.type = this.tabs[this.currentTab].type
									let res = await this.getList()
									this.list = res.data.data
									this.lastPage = res.data.last_page
								}
							}
						}
					})
				
				// let res = await getOrderApplyRefundMoney(item.id)
				// if (res.code == 1) {
				// 	this.page = 1
				// 	this.type = this.tabs[this.currentTab].type
				// 	let res = await this.getList()
				// 	this.list = res.data.data
				// 	this.lastPage = res.data.last_page
				// }

				// getOrderApplyRefundMoney(item.id).then(res=>{
				// 	if(res.code==1){
				// 		this.page = 1
				// 		this.type = this.tabs[this.currentTab].type
				// 		let res = await this.getList()
				// 		this.list = res.data.data
				// 		this.lastPage = res.data.last_page
				// 	}
				// })
			},

			//取消订单

			cancel(item) {
				// getOrderCancel
				this.$refs.cancelDialog.open()
				this.currentItem = item
			},
			async dialogCancel() {
				let ress = await getOrderCancel(this.currentItem.id)
				if (ress.code == 1) {
					uni.showToast({
						title: ress.msg,
						icon: "none"
					})

					let res = await this.getList()
					this.list = res.data.data
					this.lastPage = res.data.last_page
				}
			},
			//删除订单
			deletes(item) {
				// this.show = true;
				this.$refs.alertDialog.open()
				this.currentItem = item
			},
			dialogClose() {
				this.$refs.alertDialog.close()
				this.$refs.cancelDialog.close()
			},
			async dialogConfirm() {
				let ress = await getOrderDelete(this.currentItem.id)
				if (ress.code == 1) {
					uni.showToast({
						title: ress.msg,
						icon: "none"
					})

					let res = await this.getList()
					this.list = res.data.data
					this.lastPage = res.data.last_page
				}
			},
			//收货
			async confim(item) {
				let resConfirm = await getOrderConfirm(item.id)
				if (resConfirm.code == 1) {
					uni.showToast({
						title: resConfirm.msg,
						icon: "none"
					})
					let res = await this.getList()
					this.list = res.data.data
					this.lastPage = res.data.last_page
				}
			},
			//订单列表
			async getList() {
				let res = await getOrder(this.type, this.num, this.page)
				return res
			},
			async change(e) {
				this.currentTab = e.index
				this.page = 1
				this.type = this.tabs[this.currentTab].type
				let res = await this.getList()
				this.list = res.data.data
				this.lastPage = res.data.last_page
			},
			detail(item) {
				// uni.navigateTo({
				// 	url: '/templatePage/orderDetail/orderDetail'
				// })


				uni.navigateTo({
					url: `/templatePage/orderDetail/orderDetail?order_sn=${item.order_sn}`
				})




				// uni.navigateTo({
				// 	url: `/templatePage/productDetail/productDetail?index=${item.goods_id}`
				// })



			},
			invoiceDetail() {
				uni.navigateTo({
					url: '/templatePage/invoiceDetail/invoiceDetail'
				})
			},
			addEvaluate(item) {
				uni.navigateTo({
					url: `/templatePage/addEvaluate/addEvaluate?order_sn=${item.order_sn}`
				})
			}
		},
		onPullDownRefresh() {
			setTimeout(() => {
				uni.stopPullDownRefresh()
			}, 200);
		},
		async onReachBottom() {
			//只是测试效果，逻辑以实际数据为准
			if (this.page >= this.lastPage) {
				return
			}
			this.page += 1
			let res = await this.getList()
			// this.list = res.data.data
			this.lastPage = res.data.last_page
			if (res.data.data.length >= 1) {
				this.list = [...this.list, ...res.data.data]
			}
			// this.loadding = true
			// this.pullUpOn = true
			// setTimeout(() => {
			// 	this.loadding = false
			// 	this.pullUpOn = false
			// }, 1000)
		},
		onPageScroll(e) {
			this.scrollTop = e.scrollTop;
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		padding-bottom: env(safe-area-inset-bottom);
	}

	.tui-order-list {
		margin-top: 80rpx;
	}

	.tui-order-item {
		margin-top: 20rpx;
		border-radius: 10rpx;
		overflow: hidden;
	}

	.tui-goods-title {
		width: 100%;
		font-size: 28rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.tui-order-status {
		color: #888;
		font-size: 26rpx;
	}

	.tui-goods-item {
		width: 100%;
		padding: 20rpx 30rpx;
		box-sizing: border-box;
		display: flex;
		justify-content: space-between;
	}

	.tui-goods-img {
		width: 180rpx;
		height: 180rpx;
		display: block;
		flex-shrink: 0;
		border-radius: 20rpx;
	}

	.tui-goods-center {
		flex: 1;
		padding: 20rpx 8rpx;
		box-sizing: border-box;
	}

	.tui-goods-name {
		max-width: 310rpx;
		word-break: break-all;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
		font-size: 26rpx;
		line-height: 32rpx;
	}

	.tui-goods-attr {
		font-size: 22rpx;
		color: #888888;
		line-height: 32rpx;
		padding-top: 20rpx;
		word-break: break-all;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
	}

	.tui-price-right {
		text-align: right;
		font-size: 24rpx;
		color: #888888;
		line-height: 30rpx;
		padding-top: 20rpx;
	}

	.tui-color-red {
		color: #E41F19;
		padding-right: 30rpx;
	}

	.tui-goods-price {
		width: 100%;
		display: flex;
		align-items: flex-end;
		justify-content: flex-end;
		font-size: 24rpx;
	}

	.tui-size-24 {
		font-size: 24rpx;
		line-height: 24rpx;
	}

	.tui-price-large {
		font-size: 30rpx;
		line-height: 30rpx;
		font-weight: 500;
	}

	.tui-order-btn {
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: flex-end;
		// background: #fff;
		padding: 10rpx 30rpx 20rpx;
		box-sizing: border-box;
	}

	.tui-btn-ml {
		margin-left: 20rpx;
	}

	.marginright {
		margin-right: 10rpx;
	}

	.acBtn {
		border: 1px solid rgba(0, 0, 0, 0.2);
		padding: 10rpx 25rpx;
		color: #000;
		font-size: 22rpx;
		border-radius: 30rpx
	}
</style>